package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.minio.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.WmUserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinIoTemplate minIoTemplate;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {

        // 1. 获取图片输入流
        InputStream inputStream = multipartFile.getInputStream();

        //  设置文件名
        String contentType = multipartFile.getContentType();
        if (StringUtils.isEmpty(contentType)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
        }
        String suffixName = contentType.split("/")[1];
        String filename = System.currentTimeMillis() + "." + suffixName;
        // 2. 将输入流上传到minIO
        String url = minIoTemplate.uploadFile("", filename, inputStream, "image");

        // 3. 更新minIO的url到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmUserThreadLocal.get().getId());
        wmMaterial.setUrl(url);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterialMapper.insert(wmMaterial);
        // 4. 返回给前端页面.
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public PageResponseResult materialList(WmMaterialDto wmMaterialDto) {
        if (wmMaterialDto == null) {
            return new PageResponseResult(0,0,0);
        }

        Integer size = wmMaterialDto.getSize();
        Integer currentPage = wmMaterialDto.getPage();

        // 1. 构建查询条件
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(wmMaterialDto.getIsCollection() == 1,WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        wrapper.eq(WmMaterial::getUserId, WmUserThreadLocal.get().getId());

        // 2. 构建分页条件
        Page<WmMaterial> page = new Page<>(currentPage,size);

        // 3. 执行分页查询
        Page<WmMaterial> materialPage = wmMaterialMapper.selectPage(page, wrapper);

        Long total = materialPage.getTotal();
        PageResponseResult result = new PageResponseResult(currentPage,size,total.intValue());
        result.setData(materialPage.getRecords());
        // 4. 返回
        return result;
    }
}
